package main

import "fmt"

// Author : CSR丶WARRIOR
// Time ： 2020/10/14  20:18
// TOOLS : GoLand
// FILENAME : structArray.go
// STATEMENT: 结构体与数组

type Person struct {
	id   int
	name string
	age  int
	addr string
}

func createArray() {
	var p1 Person = Person{
		id:   1,
		name: "张三",
		age:  20,
		addr: "北京",
	}
	var p2 Person = Person{
		id:   2,
		name: "李四",
		age:  23,
		addr: "上海",
	}
	var p3 Person = Person{
		id:   3,
		name: "王五",
		age:  30,
		addr: "广州",
	}
	var personArray [3]Person = [3]Person{p1, p2, p3}
	fmt.Println(personArray[0].age) //张三的age 20
	fmt.Println(personArray)        //[{1 张三 20 北京} {2 李四 23 上海} {3 王五 30 广州}]

	//修改属性:李四的年龄
	personArray[1].age = 32
	fmt.Println(personArray) //[{1 张三 20 北京} {2 李四 32 上海} {3 王五 30 广州}]
}

func traversal() {
	var p1 Person = Person{
		id:   1,
		name: "张三",
		age:  20,
		addr: "北京",
	}
	var p2 Person = Person{
		id:   2,
		name: "李四",
		age:  23,
		addr: "上海",
	}
	var p3 Person = Person{
		id:   3,
		name: "王五",
		age:  30,
		addr: "广州",
	}
	var personArray [3]Person = [3]Person{p1, p2, p3}
	for _, person := range personArray {
		fmt.Printf("name:%s id:%d age:%d address:%s\n", person.name, person.id, person.age, person.addr)
	}
}
func main() {
	traversal()
}
